package com.echat.ebluetooth.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.echat.ebluetooth.R;
import com.echat.ebluetooth.bean.Beacon;

import java.util.List;

public class BeaconAdapter extends RecyclerView.Adapter<BeaconAdapter.ViewHolder> {

    private List<Beacon> mBeaconList;

    public BeaconAdapter(List<Beacon> beaconList) {
        this.mBeaconList = beaconList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_device, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Beacon beacon = mBeaconList.get(position);
        holder.tvName.setText(beacon.getName());
        holder.tvAddress.setText(beacon.getAddress());
        holder.tvRssi.setText(new StringBuilder().append(beacon.getRssi()).append("dbm"));
    }

    @Override
    public int getItemCount() {
        return mBeaconList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        TextView tvName;
        TextView tvAddress;
        TextView tvRssi;

        public ViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvAddress = (TextView) itemView.findViewById(R.id.tv_address);
            tvRssi = (TextView) itemView.findViewById(R.id.tv_rssi);
        }
    }
}
